/****************************************************************************************************
Name:  updatecaseowner ()
Copyright © 2012  Lodha Group
=====================================================================================================
=====================================================================================================
Purpose: 
-------
To Update Case Owner same as Account owner
=====================================================================================================
=====================================================================================================
History
-----------------------------------------------------------------------------------------------------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          10/04/2012      INITIAL DEVELOPMENT  
******************************************************************************************************/

public class UpdateCaseOwner{

 /*******************************************************************
    Purpose     : To change Case owner to reflect Account Owner                        
    Parameters  : List of Case
    Returns     : Null
  ********************************************************************/   
    
    
   public void Updatecaseownerafterinsert(List<Case> caselist)
        {
        try {

            for(Case c: caselist)
                { 
                  Account a=[select id,owner.Id,name from Account where Owner_Name__c=:c.Account_Owner__c limit 1];
                
                     if(a.owner.id!=c.ownerId) 
                     {                         
                      c.ownerId=a.owner.id;
                      update c;
                     }        
                }
              }catch(Exception e){
           }          
        } 
  /*******************************************************************
    Purpose     : To change AllOpen Cases owner to reflect Current Owner                        
    Parameters  : List of Case
    Returns     : Null
  ********************************************************************/  
  
  
  public void updateopencaseowner(List<Account> oldaccountlist,List<Account> newaccountlist)
      {
       system.debug('$$OLDIDLISt'+oldaccountlist);
          Set<id> oldaccidset=new Set<Id>(); 
          List<Case> casestoupdate=new List<Case>();
          for(Account acc: oldaccountlist)
          {
              oldaccidset.add(acc.OwnerId);
          }
        system.debug('@@@SET'+oldaccidset);
        
              List<Case> listofopencases=[select id,OwnerId,Status from Case where (Status='New'
              OR Status='Escalated' OR Status='On Hold') AND Owner.id=:oldaccidset];
             
             system.debug('QUERy LIST'+listofopencases); 
              
              for(Account accnew: newaccountlist)
              {
                  if(!oldaccidset.contains(accnew.owner.id))              
                  {
                  
                  for(Case clist: listofopencases)
                      {                 
                          clist.ownerId=accnew.OwnerId;
                          
                         casestoupdate.add(clist);
                     }
                     
                  }
              
              }
              system.debug('ToUPDATE'+casestoupdate);
              update casestoupdate;
             
          }
          
      }